/*
ID: pallab81
PROG:
LANG: C++
*/

//"I have not failed, I have just found 10000 ways that won't work.

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define foR(i,st,ed) for(int i = st ; i < ed ; ++i )
#define fo(i,ed) foR( i , 0 , ed )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

class OnTime {
public:
    int minCost(int N, int T, vector <string> buses);
};
int n;
int tt;
int nbus;
struct NODE {
    int u;
    int v;
    int dep_time;
    int tim_take;
    int cost;
    NODE(int u=0, int v=0,int dep_time=0,int tim_take=0, int cost=0)
            :u(u),v(v),dep_time(dep_time),tim_take(tim_take),cost(cost) {
        ;
    }
};

vector< NODE > BUS;
inline NODE parse(string str) {
    int a,b,c,d,e;
    sscanf(str.c_str(),"%d %d %d %d %d",&a,&b,&c,&d,&e);
    return NODE(a,b,c,d,e);
}
#define INF 1023456789
int dp[51][10001];
int go(int u, int t) {
    if ( t > tt )return INF;

    int &best = dp[u][t];
    if ( best!=-1 )return best;

    if ( u==(n-1) ) {
        return 0 ;
    }
    best = INF;
    fo(i,nbus) {
        int u_ = BUS[i].u;
        int v_ = BUS[i].v;
        int dep_time_ = BUS[i].dep_time;
        int tim_take_ = BUS[i].tim_take;
        int cost_ = BUS[i].cost;
        if ( u==u_  && t<dep_time_ ) {
            int v = cost_+go( v_, dep_time_+tim_take_ );
            best = min( best, v );
        }
    }
    return best;
}

int OnTime :: minCost(int N, int T, vector <string> buses) {

    n = N;
    tt = T;
    BUS.clear();
    nbus = SZ( buses );

    fo(i,nbus) {
        BUS.pb( parse(buses[i]) );
    }
    SET(dp,-1);
    int retval=INF;
    fo(i,nbus) {
        if ( BUS[i].u==0 ) {
            int v = BUS[i].cost+go( BUS[i].v, BUS[i].dep_time+BUS[i].tim_take  );
            retval = min( retval, v );
        }
    }
    printf("RESULT = %d\n",retval );
    return retval < INF ? retval : -1;
}
// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit 2.1.8 (beta) modified by pivanof
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool KawigiEdit_RunTest(int testNum, int p0, int p1, vector <string> p2, bool hasAnswer, int p3) {
    cout << "Test " << testNum << ": [" << p0 << "," << p1 << "," << "{";
    for (int i = 0; int(p2.size()) > i; ++i) {
        if (i > 0) {
            cout << ",";
        }
        cout << "\"" << p2[i] << "\"";
    }
    cout << "}";
    cout << "]" << endl;
    OnTime *obj;
    int answer;
    obj = new OnTime();
    clock_t startTime = clock();
    answer = obj->minCost(p0, p1, p2);
    clock_t endTime = clock();
    delete obj;
    bool res;
    res = true;
    cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
    if (hasAnswer) {
        cout << "Desired answer:" << endl;
        cout << "\t" << p3 << endl;
    }
    cout << "Your answer:" << endl;
    cout << "\t" << answer << endl;
    if (hasAnswer) {
        res = answer == p3;
    }
    if (!res) {
        cout << "DOESN'T MATCH!!!!" << endl;
    } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
        cout << "FAIL the timeout" << endl;
        res = false;
    } else if (hasAnswer) {
        cout << "Match :-)" << endl;
    } else {
        cout << "OK, but is it right?" << endl;
    }
    cout << "" << endl;
    return res;
}
int main() {
    bool all_right;
    all_right = true;

    int p0;
    int p1;
    vector <string> p2;
    int p3;

    {
        // ----- test 0 -----
        p0 = 3;
        p1 = 8;
        string t2[] = {"0 1 0 4 3","1 2 5 3 4"};
        p2.assign(t2, t2 + sizeof(t2) / sizeof(t2[0]));
        p3 = 7;
        all_right = KawigiEdit_RunTest(0, p0, p1, p2, true, p3) && all_right;
        // ------------------
    }

    {
        // ----- test 1 -----
        p0 = 3;
        p1 = 8;
        string t2[] = {"0 1 0 4 3","1 2 6 3 4"};
        p2.assign(t2, t2 + sizeof(t2) / sizeof(t2[0]));
        p3 = -1;
        all_right = KawigiEdit_RunTest(1, p0, p1, p2, true, p3) && all_right;
        // ------------------
    }

    {
        // ----- test 2 -----
        p0 = 3;
        p1 = 7;
        string t2[] = {"0 1 0 5 1","1 2 6 1 40","0 1 1 2 5","1 2 4 2 5"};
        p2.assign(t2, t2 + sizeof(t2) / sizeof(t2[0]));
        p3 = 10;
        all_right = KawigiEdit_RunTest(2, p0, p1, p2, true, p3) && all_right;
        // ------------------
    }

    {
        // ----- test 3 -----
        p0 = 3;
        p1 = 8;
        string t2[] = {"0 1 0 5 3","1 2 5 3 4"};
        p2.assign(t2, t2 + sizeof(t2) / sizeof(t2[0]));
        p3 = -1;
        all_right = KawigiEdit_RunTest(3, p0, p1, p2, true, p3) && all_right;
        // ------------------
    }

    {
        // ----- test 4 -----
        p0 = 3;
        p1 = 100;
        string t2[] = {"0 1 0 1 1"};
        p2.assign(t2, t2 + sizeof(t2) / sizeof(t2[0]));
        p3 = -1;
        all_right = KawigiEdit_RunTest(4, p0, p1, p2, true, p3) && all_right;
        // ------------------
    }

    {
        // ----- test 5 -----
        p0 = 9;
        p1 = 100;
        string t2[] = {"0 3 1 6 15","0 6 0 23 20","6 2 25 15 30","6 1 30 15 40","3 1 15 35 10","3 2 30 80 40","1 5 55 25 25","1 2 49 31 10","2 8 85 10 10","5 8 83 15 5"};
        p2.assign(t2, t2 + sizeof(t2) / sizeof(t2[0]));
        p3 = 55;
        all_right = KawigiEdit_RunTest(5, p0, p1, p2, true, p3) && all_right;
        // ------------------
    }

    if (all_right) {
        cout << "You're a stud (at least on the example cases)!" << endl;
    } else {
        cout << "Some of the test cases had errors." << endl;
    }
    return 0;
}
// END KAWIGIEDIT TESTING


//Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!
// kate: indent-mode cstyle; space-indent on; indent-width 0; 
